USE [BMIG_MENSUAL_POST_CADENA]

 -- creating the store procedure
IF EXISTS (SELECT name 
	   FROM   sysobjects 
	   WHERE  name = 'WASP_M0009_PRODUCCION_CUOTAS'
	   AND 	  type = 'P') 
	DROP PROCEDURE [dbo].[WASP_M0009_PRODUCCION_CUOTAS]
GO
CREATE PROCEDURE [dbo].[WASP_M0009_PRODUCCION_CUOTAS]
(@FECHA_INICIO CHAR(8), @FECHA_TERMINO AS CHAR(8)) AS
begin      
      SELECT  a.ppsuc
            , a.ppcta
            , a.ppoper
            , a.ppsbop
            , SUM(b.ppcap + b.ppint) AS total_credito
            , COUNT(b.ppfvto) AS totcuotas
      into  dbo.Produccion_Cuotas_ant      
      FROM  dbo.fsd611 AS a WITH (nolock) 
            CROSS JOIN dbo.fsd601 AS b WITH (nolock) 
            CROSS JOIN dbo.fsd010 AS c WITH (nolock) 
            CROSS JOIN dbo.ingreso_produccion_seguro_asisten_ant AS d 
            CROSS JOIN dbo.JT73109 AS e WITH (nolock)
      WHERE (a.pptipo = '') 
            AND (a.ppexte = 0)
            AND (a.pgcod = b.pgcod) 
            AND (a.ppmod = b.ppmod) 
            AND (a.ppsuc = b.ppsuc) 
            AND (a.ppmda = b.ppmda) 
            AND (a.ppcta = b.ppcta) 
            AND (a.ppoper = b.ppoper) 
            AND (a.ppoper = c.aooper) 
            AND (a.ppcta = c.aocta) 
            AND (a.ppsuc = c.aosuc) 
            AND (a.ppsbop = b.ppsbop) 
            AND (a.pptope = b.pptope) 
            AND (a.ppsuc = b.ppsuc) 
            AND (a.ppmda = b.ppmda) 
            AND (a.ppcta = b.ppcta) 
            AND (a.ppoper = b.ppoper) 
            AND (a.ppsuc = d.ppsuc) 
            AND (a.ppcta = d.ppcta) 
            AND (a.ppoper = d.ppoper) 
            AND (b.d601co = 'S') 
            AND (c.aosbop = 0) 
            AND (e.JT73109FPA = a.ppoper) 
            AND (e.JT73109SUC = a.ppsuc) 
            AND (e.JT73109FAP BETWEEN @FECHA_INICIO AND @FECHA_TERMINO)
      GROUP BY a.ppsuc, a.ppcta, a.ppoper, a.ppsbop
      ORDER BY a.ppcta
END      



